
package com.insigma.business.qrcode.common.util;


import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


public final class BizCheckUtil {

    private BizCheckUtil(){}

    public static void checkIsNull(final Object obj, String... errorMsgArray) {
        if (obj != null) {
            if (errorMsgArray != null) {
                final StringBuilder stringBuilder = new StringBuilder();
                for (String errorMsg : errorMsgArray) {
                    stringBuilder.append(errorMsg);
                }
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
            } else {
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "对象不能为空");
            }
        }
    }
    private static final String REGEX_MOBILE = "((^(13|15|16|18|17|19)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}-(\\d{1,4})$))";


    private static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";



    private static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";


    private static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";


    private static final String REGEX_IP_ADDR = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";

    public static void checkMobile(final String mobile) {
        if(mobile==null || !Pattern.matches(REGEX_MOBILE, mobile)){
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "错误的手机号");
        }
        return;
    }


    public static void checkEmail(final String email) {
        if(email==null || !Pattern.matches(REGEX_EMAIL, email)){
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "错误的邮箱");
        }
    }


    public static void checkIDCardNO(final String idCard) {
        if(idCard==null || !Pattern.matches(REGEX_ID_CARD, idCard)){
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "错误的身份证号码");
        }
    }


    public static void checkUrl(final String url) {
        if(url==null || !Pattern.matches(REGEX_URL, url)){
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "错误的URL格式");
        }
    }


    public static void checkIP(final String ipAddress) {
        if(ipAddress==null || !Pattern.matches(REGEX_IP_ADDR, ipAddress)){
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "错误的IP格式");
        }
    }


    public static void checkNotBlank(final String str, String... errorMsgArray){
        if(StringUtils.isBlank(str) ){
            if(errorMsgArray!=null){
                final StringBuilder stringBuilder = new StringBuilder();
                for(String errorMsg:errorMsgArray){
                    stringBuilder.append(errorMsg);
                }
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
            } else{
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "字符串不能为空");
            }
        }
    }


    public static void checkEqual(final String str1,final String str2 , String... errorMsgArray){
        if(!StringUtils.equals(str1,str2) ){
            if(errorMsgArray!=null){
                final StringBuilder stringBuilder = new StringBuilder();
                for(String errorMsg:errorMsgArray){
                    stringBuilder.append(errorMsg);
                }
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
            } else{
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "不相等");
            }
        }
    }


    public static void checkNotNull(final Object obj, String... errorMsgArray){
        if(obj==null){
            if(errorMsgArray!=null){
                final StringBuilder stringBuilder = new StringBuilder();
                for(String errorMsg:errorMsgArray){
                    stringBuilder.append(errorMsg);
                }
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
            } else{
                throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "对象不能为空");
            }
        }
    }



    public static void uniqueIdCheck(final String uniqueId) {
        if (StringUtils.isBlank(uniqueId)) {
            throw new BizException(ErrorCodeEnum.ERROR_UNIQUE_ID_EMPTY,"幂等控制编号必填");
        }

        if (uniqueId.length() > CommonConstant.COMMON_UNIQUEID_MAX_LENGTH) {
            throw new BizException(
                    ErrorCodeEnum.ERROR_UNIQUEID_LENGTH_OUT_OF_BOUNDS,"幂等编号超过最大长度");
        }

        notChineseCheck(uniqueId);
    }


    public static void notChineseCheck(final String text) {
        if (text.matches(".*[\u4e00-\u9fa5].*?")) {
            throw new BizException(ErrorCodeEnum.ERROR_NOT_CHINESE,"不能包含汉字");
        }
    }


    public static int getLength(final String text) {
        int length = 0;
        if (!StringUtils.isBlank(text)) {
            for (int i = 0; i < text.length(); i++) {
                if (isChinese(text.charAt(i))) {
                    length += 3;
                } else {
                    length += 1;
                }
            }
        }

        return length;
    }


    private static boolean isChinese(final char ch) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
        if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }

        return false;
    }


    public static void checkPageNum(final int pageNum) {
        if (pageNum < 1) {
            throw new BizException(ErrorCodeEnum.ERROR_PAGE_NUM,"页码错误-页码需要大于1");
        }
    }


    public static void checkPageSize(final int pageSize) {
        if (pageSize < 1 || pageSize > CommonConstant.MAX_PAGE_SIZE) {
            throw new BizException(ErrorCodeEnum.ERROR_PAGE_SIZE, "页大小错误-范围不在1-1000内");
        }
    }




    public static void checkIsNotEmptyAndRepeat(final String[] strArray,final String errorMsg){
        if(ArrayUtils.isEmpty(strArray)){
            throw new BizException(ErrorCodeEnum.ERROR_EMPTY_OR_REPEATED,
                    StringUtils.isBlank(errorMsg)?"对象数组为空或者内容重复了":errorMsg);
        }else {
            final List<String> uniqueValueList = new ArrayList<String>(strArray.length);
            for(String str:strArray){
                if(uniqueValueList.contains(str)){
                    throw new BizException(ErrorCodeEnum.ERROR_EMPTY_OR_REPEATED,
                            StringUtils.isBlank(errorMsg)?"对象数组为空或者内容重复了":errorMsg);
                }
                uniqueValueList.add(str);
            }
        }
    }


    public static void checkIsTrue(final boolean boolExpression, final BizException cbe) {
        if (!boolExpression) {
            throw cbe;
        }
    }




    public static void checkIsTrue(final boolean boolExpression, final String... errorMsgArray) {
        final StringBuilder stringBuilder = new StringBuilder();

        if(ArrayUtils.isNotEmpty(errorMsgArray)) {
            for(String str:errorMsgArray){
                stringBuilder.append(str);
            }
        }

        if (!boolExpression) {
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
        }
    }

    public static void checkIsTrue(final boolean boolExpression, final ScenarioCodeEnum scenarioCodeEnum, final String... errorMsgArray) {
        final StringBuilder stringBuilder = new StringBuilder();

        if(ArrayUtils.isNotEmpty(errorMsgArray)) {
            for(String str:errorMsgArray){
                stringBuilder.append(str);
            }
        }

        if (!boolExpression) {
            throw new BizException(scenarioCodeEnum, ErrorCodeEnum.COMMON_INNER_ERROR, stringBuilder.toString());
        }
    }


    public static void checkIsTrue(final boolean boolExpression, final ScenarioCodeEnum scenarioCodeEnum, final ErrorCodeEnum resultDetailCodeEnum, final String... errorMsgArray) {
        final StringBuilder stringBuilder = new StringBuilder();

        if(ArrayUtils.isNotEmpty(errorMsgArray)) {
            for(String str:errorMsgArray){
                stringBuilder.append(str);
            }
        }

        if (!boolExpression) {
            if (resultDetailCodeEnum==null) {
                throw new BizException(scenarioCodeEnum, ErrorCodeEnum.COMMON_INNER_ERROR, "传入的错误码枚举不能为空,并且："+stringBuilder.toString());
            }

            throw new BizException(scenarioCodeEnum, resultDetailCodeEnum, stringBuilder.length()==0?resultDetailCodeEnum.getDesc():stringBuilder.toString());
        }
    }


    public static void checkNumberBound(final Number num, final Number maxNum,
                                        final BizException cbe) {

        if (num == null || maxNum == null) {
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,
                    "待比较数字或阈值数字为null");
        }

        if (NumberUtils.min(num.doubleValue(), maxNum.doubleValue()) == maxNum
                .doubleValue()) {
            throw cbe;
        }
    }


}
